package com.wyt.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyt.dao.DepartmentMapper;
import com.wyt.entity.Department;
import com.wyt.until.CommonResult;
import com.wyt.until.TableLayui;
import javafx.scene.DepthTest;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: xiangmu01
 * @description: 部门表的增删改查
 * @author: 王一同
 * @create: 2021-11-22 09:28
 **/

@RestController
public class ShowDepartment {
    @Autowired
    private DepartmentMapper deptMapper;

    /**
    	* @Description:    模块作用描述
    	* @Author:         wyt
    	* @CreateDate:     2021/11/22 10:02
        * @UpdateRemark:   特殊说明
    	*/

    @RequestMapping("ShowDepartment")
    public TableLayui ShowDepartment(Integer page, Integer limit,Integer departmentId,String departmentName, String departmentDisc){
        PageHelper.startPage(page,limit);
        List<Department> likeselect = deptMapper.likeselect(departmentId, departmentName, departmentDisc);
        PageInfo<Department> pageInfo=new PageInfo<Department>(likeselect);
        TableLayui tableLayui = new TableLayui(0, "",  pageInfo.getTotal(), pageInfo.getList());
        return tableLayui;
    }

    /**
    	* @Description:    删除当前行
    	* @Author:         wyt
    	* @CreateDate:     2021/11/22 11:32
        * @UpdateRemark:   未完成
    	*/

    @RequestMapping("deleteDepartment")
    public CommonResult deleteDepartment(String departmentId){
        Integer renshu = deptMapper.selectrenshu(departmentId);
        CommonResult commonResult = new CommonResult();
        if(renshu!=0){
            commonResult.setCode(500);
            commonResult.setMsg("不可以删除哦，该部门还有"+renshu+"个人");
            return commonResult;
        }else{
            int i = deptMapper.deleteBydepartmentId(departmentId);
            if(i>=1){
                commonResult.setCode(200);
                commonResult.setMsg("删除成功");
                return commonResult;
            }else{
                commonResult.setCode(500);
                commonResult.setMsg("删除失败");
                return commonResult;
            }
        }

    }

    @RequestMapping("insertDepartment")
    public CommonResult insertDepartment(Integer departmentId,String departmentName,Integer dstatus,String departmentDisc){
        Integer list = deptMapper.selectBydepartmentId(departmentId);
        CommonResult commonResult = new CommonResult();
        if(list==null){
            int insert = deptMapper.insert(departmentId,departmentName,dstatus,departmentDisc);
            if(insert>=1){
                commonResult.setCode(200);
                commonResult.setMsg("添加成功");
                return commonResult;
            }else{
                commonResult.setCode(500);
                commonResult.setMsg("添加失败");
                return commonResult;
            }
        }else{
            commonResult.setCode(500);
            commonResult.setMsg("部门编号重复");
            return commonResult;
        }

    }

    @RequestMapping("updateDepartment")
    public CommonResult updateDepartment(Department department){
        int i = deptMapper.updateByDept(department);
        CommonResult commonResult = new CommonResult();
        if(i>=1){
            commonResult.setCode(200);
            commonResult.setMsg("修改成功");
            return commonResult;
        }else{
            commonResult.setCode(500);
            commonResult.setMsg("修改失败");
            return commonResult;
        }

    }

    @RequestMapping("pldeleteDepartment")
    public CommonResult pldeleteDepartment(String[] arrayid) {
        CommonResult commonResult = new CommonResult();
        for (String id : arrayid) {
            Integer renshu = deptMapper.selectrenshu(id);
            if (renshu != 0) {
                commonResult.setCode(500);
                commonResult.setMsg("不可以删除哦，" + "编号为" + id + "的部门还有" + renshu + "个人");
                return commonResult;
            } else {
                int i = deptMapper.deleteBydepartmentId(id);
                if (i >= 1) {
                    commonResult.setCode(200);
                    commonResult.setMsg("删除成功");
                    return commonResult;
                } else {
                    commonResult.setCode(500);
                    commonResult.setMsg("删除失败");
                    return commonResult;
                }
            }

        }
        return commonResult;
    }
}
